library basicnetservice;

import 'dart:convert';
import 'dart:developer';
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart' as LCGet;
import 'package:smart_freezer/core/service/service/dio_instance.dart';
import 'package:smart_freezer/core/service/service/result_data.dart';
import 'package:smart_freezer/core/utils/logs.dart';

import 'app_net_service.dart';
import 'dialog_param.dart';
import 'loading_dialog.dart';

class AppApi extends AppNetService {

  static const String customer = "/freezer-app-api";

  /// 获取短信验证码
  static const String _Login_SMS_Email = customer + "/login/sendCode";
  /// 登录
  static const String _Login_Login = customer + "/login/login";
  /// 忘记密码
  static const String _Login_Reset_Pwd = customer + "/login/forget";
  /// 注册
  static const String _Login_Register_Info = customer + "/login/register";
  /// 用户协议
  static const String _Login_Register_Agree = customer + "/login/userAgreement";

  /// 轮播图
  static const String _Home_Index_Banner = customer + "/index/banner";
  /// 楼层列表
  static const String _Home_Index_Floor = customer + "/index/floor";
  /// 房层列表
  static const String _Home_Index_Hall = customer + "/index/hall";

  /// 设备列表
  static const String _Home_Product_List = customer + "/product/list";
  /// 设备信息
  static const String _Home_Product_Info = customer + "/product/info";
  /// 绑定设备
  static const String _Home_Product_Bind = customer + "/product/bind";
  /// 修改设备名称
  static const String _Home_Product_UpdateName = customer + "/product/updateName";
  /// 修改经销商名称
  static const String _Home_Product_UpdateAgencyName = customer + "/product/updateAgencyName";
  /// 修改设备位置
  static const String _Home_Product_UpdateLocation = customer + "/product/updateLocation";

  /// 设备分享列表
  static const String _Home_Share_List = customer + "/productShare/list";
  /// 设备分享信息
  static const String _Home_Share_Info = customer + "/productShare/info";
  /// 保存分享
  static const String _Home_Share_Save = customer + "/productShare/saveShare";
  /// 接受邀请
  static const String _Home_Share_Accept = customer + "/productShare/acceptRefuse";
  /// 删除分享
  static const String _Home_Share_Delete = customer + "/productShare/deleteShare";
  /// 编辑分享
  static const String _Home_Share_Update = customer + "/productShare/updateShare";

  /// 参数设置信息
  static const String _Home_Setting_Info = customer + "/productSetting/info";
  /// 修改参数值
  static const String _Home_Setting_UpdateParamValue = customer + "/productSetting/updateParamValue";
  /// 温度列表
  static const String _Home_Temp_Data = customer + "/productTemp/data";
  /// 修改参数值
  static const String _Home_Temp_Info = customer + "/productTemp/info";
  /// 收藏设备列表
  static const String _Home_Collect_List = customer + "/collect/list";
  /// 新增收藏设备
  static const String _Home_Collect_Collect = customer + "/collect/collect";
  /// 删除收藏设备
  static const String _Home_Collect_Remove = customer + "/collect/remove";

  /// 报警信息
  static const String _Home_Warn_List = customer + "/productWarning/list";

  /// 保存极光推送注册Id
  static const String _Login_RegisterId_Save = customer + "/index/saveRegisterId";

  /// 上传文件
  static const String _Mine_Upload_Img = "/fileUpload/saveSingle";


  AppApi._();

  static AppApi? _instance;

  static AppApi getInstance() {
    /*
    if (_instance == null) {
      _instance = AppApi._();
    }
     */
    _instance ??= AppApi._();
    return _instance!;
  }


  /// 保存极光推送注册id
  Future<ResultData> saveRegisterId(Map<String, String> params) async {
    ResultData resultData = await get(_Login_RegisterId_Save, params: params);
    return resultData;
  }

  /// 开始登录
  Future<ResultData> startLogin(Map<String, String> params) async {
    ResultData resultData = await get(_Login_Login, params: params);
    return resultData;
  }
  /// 注册
  Future<ResultData> startRegister(Map<String, String> params) async {
    ResultData resultData = await get(_Login_Register_Info, params: params);
    return resultData;
  }
  /// 发送验证码
  Future<ResultData> sendSMS(Map<String, String> params) async {
    ResultData resultData = await get(_Login_SMS_Email, params: params);
    return resultData;
  }
  /// 找回密码
  Future<ResultData> resetPwd(Map<String, String> params) async {
    ResultData resultData = await get(_Login_Reset_Pwd, params: params);
    return resultData;
  }
  /// 用户协议
  Future<ResultData> getRegisterAgree() async {
    ResultData resultData = await get(_Login_Register_Agree);
    return resultData;
  }
  /// 轮播图信息
  Future<ResultData> getBannerInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Index_Banner, params: params);
    return resultData;
  }
  /// 楼层列表
  Future<ResultData> getFloorInfo() async {
    ResultData resultData = await get(_Home_Index_Floor);
    return resultData;
  }
  /// 房间列表
  Future<ResultData> getHallInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Index_Hall, params: params);
    return resultData;
  }
  /// 设备列表
  Future<ResultData> getProductList(Map<String, String> params) async {
    var result = await get(_Home_Product_List, params: params);
    if(result == null){
      return ResultData();
    }
    ResultData resultData = result;
    return resultData;
  }
  /// 设备信息
  Future<ResultData> getProductInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Product_Info, params: params);
    return resultData;
  }
  /// 绑定设备
  Future<ResultData> bindProduct(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Product_Bind, params: params);
    return resultData;
  }
  /// 修改设备名称
  Future<ResultData> updateProductName(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Product_UpdateName, params: params);
    return resultData;
  }
  /// 修改经销商名称
  Future<ResultData> updateAgencyName(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Product_UpdateAgencyName, params: params);
    return resultData;
  }
  /// 修改设备位置
  Future<ResultData> updateLocation(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Product_UpdateLocation, params: params);
    return resultData;
  }
  /// 分享列表
  Future<ResultData> getShareList(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_List, params: params);
    return resultData;
  }
  /// 分享信息
  Future<ResultData> getShareInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_Info, params: params);
    return resultData;
  }
  /// 保存分享
  Future<ResultData> saveShareInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_Save, params: params);
    return resultData;
  }
  /// 接受/拒绝邀请
  Future<ResultData> acceptShareInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_Accept, params: params);
    return resultData;
  }
  /// 删除分享
  Future<ResultData> deleteShareInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_Delete, params: params);
    return resultData;
  }
  /// 编辑分享
  Future<ResultData> updateShareInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Share_Update, params: params);
    return resultData;
  }

  /// 参数设置信息
  Future<ResultData> getSettingInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Setting_Info, params: params);
    return resultData;
  }
  /// 修改参数值
  Future<ResultData> updateParamValue(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Setting_UpdateParamValue, params: params);
    return resultData;
  }
  /// 获取温度记录
  Future<ResultData> getTempList(Map<String, String> params) async {
    final result = await get(_Home_Temp_Data, params: params);
    if(result != null){
      ResultData resultData = result;
      return resultData;
    }else{
      return ResultData();
    }
  }
  /// 获取温度信息
  Future<ResultData> getTempInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Temp_Info, params: params);
    return resultData;
  }
  /// 获取收藏设备列表
  Future<ResultData> getCollectList(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Collect_List, params: params);
    return resultData;
  }
  /// 新增收藏设备
  Future<ResultData> addCollectInfo(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Collect_Collect, params: params);
    return resultData;
  }
  /// 删除收藏设备
  Future<ResultData> removeCollectList(Map<String, String> params) async {
    ResultData resultData = await get(_Home_Collect_Remove, params: params);
    return resultData;
  }

  /// 获取报警信息
  Future<ResultData> getWarnList(Map<String, String> params) async {
    final result = await get(_Home_Warn_List, params: params);
    if(result != null){
      ResultData resultData = result;
      return resultData;
    }else{
      return ResultData();
    }

    ResultData resultData = await get(_Home_Warn_List, params: params);
    return resultData;

  }

  // /// 退出登录
  // Future<ResultData> startLogout() async {
  //   ResultData resultData = await post_normal(_Login_Logout);
  //   return resultData;
  // }
  // /// 获取验证码
  // Future<ResultData> getEmailCode(Map<String, String> params) async {
  //   ResultData resultData = await post_normal(_Login_SMS_Email, params: params);
  //   return resultData;
  // }
  //
  // /// 重置密码
  // Future<ResultData> resetPassword(Map<String, String> params) async {
  //   ResultData resultData = await post_normal(_Login_Reset_Pwd, params: params);
  //   return resultData;
  // }
  //

  // /// 开始注册
  // Future<ResultData> startRegisterInfo(Map<String, dynamic> params) async {
  //   ResultData resultData = await post(_Login_Register_Info, params: params);
  //   return resultData;
  // }

  /// 轮播图
  Future<ResultData> getBannerData() async {
    ResultData resultData = await get(_Home_Index_Banner);
    return resultData;
  }

  post_normal(String url, { Map<String, dynamic>? params }) async {
    Dio dioInstance = DioInstance.createInstance();
    var baseUrl = await getBasicUrl();
    var header = await getHeaders();
    dioInstance.options.baseUrl = baseUrl;
    if(header != null) {
      dioInstance.options.headers = header;
    }
    Response response = await dioInstance.post(url, queryParameters: params,);
    Map<String, dynamic> data = json.decode(response.data);

    logs("当前url: $url, params: $params"
        "返回结果: $data");

    return ResultData.response(data);
  }

  post_type(String url, { Map<String, dynamic>? params, String contentType = "application/json" }) async {
    Dio dioInstance = DioInstance.createInstance();
    var baseUrl = await getBasicUrl();
    var header = await getHeaders();
    dioInstance.options.baseUrl = baseUrl;
    if(header != null) {
      dioInstance.options.headers = header;
    }
    dioInstance.options.contentType = contentType;
    Response response = await dioInstance.post(url, data: params,);
    Map<String, dynamic> data = json.decode(response.data);
    return ResultData.response(data);
  }


  // /// 获取摄像头信息
  // Future<ResultData> getCameraInfo(Map<String, dynamic> params) async {
  //   ResultData resultData = await get(_Home_Camera_List, params: params);
  //   return resultData;
  // }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


  /// 上传头像接口
  Future<ResultData> uploadImage(BuildContext context, File file, {Map<String, String>? params}) async {
    // 开始进度
    ShowParam showParam =
    ShowParam(barrierDismissible: true, showBackground: true);
    showParam.text = "Uploading...";
    LoadingDialogUtil.showTextLoadingDialog(context, showParam);
    String fileName = file.path.substring(file.path.lastIndexOf("/") + 1);
    print("filename: $fileName");
    List<int> bytes = await file.readAsBytes();
    ResultData resultData = await upLoad(file, fileName, _Mine_Upload_Img, params: params);

    showParam.pop();
    // 结束进度
    return resultData;
  }



}
